package com.ahstu.learning.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.ahstu.learning.R;
import com.ahstu.learning.base.BaseActivity;
import com.ahstu.learning.bean.User;
import com.ahstu.learning.model.UserModel;


public class SplashActivity extends BaseActivity implements Animation.AnimationListener {
    private ImageView imageView;
    private Animation animation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        imageView = (ImageView) findViewById(R.id.splash_img);
        animation = AnimationUtils.loadAnimation(this, R.anim.welcome);
        animation.setFillEnabled(true);
        animation.setFillAfter(true);
        imageView.setAnimation(animation);
        animation.setAnimationListener(this);

    }

    @Override
    protected void onNewIntent(Intent intent) {
        // TODO Auto-generated method stub
        super.onNewIntent(intent);
        if ((Intent.FLAG_ACTIVITY_CLEAR_TOP & intent.getFlags()) != 0) {
            finish();
        }
    }


    @Override
    public void onAnimationStart(Animation animation) {

    }

    @Override
    public void onAnimationEnd(Animation animation) {
        User user = UserModel.getInstance().getCurrentUser();
        if (user == null) {
//                    startActivity(LoginActivity.class, null, true);
            startActivity(new Intent(SplashActivity.this, LoginActivity.class));
            finish();
        } else {
//                    startActivity(MainActivity.class, null, true);
            startActivity(new Intent(SplashActivity.this, MainActivity.class));
            finish();
        }

    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return false;
        }
        return false;
    }
}
